<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/5/28
 * Time: 16:39
 */
namespace App\Modules\User\Services;
use App\Modules\User\Models\User;
use Illuminate\Support\Facades\Log;
use Redis;
use App\Modules\User\Models\UserTaskDetail;

class UserTaskDetailService {

    /**
     * 获取用户打赏次数及打赏金额
     * @param $uid
     * @return mixed
     */
    static function getUserRewardsInfo($uid){
        $rewardTimes = Redis::hget('user_task_info:'.$uid,'rewards_count');
        $rewardTotal = Redis::hget('user_task_info:'.$uid,'rewards_total_fee');
        if($rewardTimes===null || $rewardTotal===null) {
            $info = UserTaskDetail::getUserRewardsInfo($uid);
            $rewardTimes = $info['rewardTimes'];
            $rewardTotal = $info['rewardTotal'];
            Redis::hset('user_task_info:'.$uid,'rewards_count',$rewardTimes);
            Redis::hset('user_task_info:'.$uid,'rewards_total_fee',$rewardTotal);
        }
        return ['rewardTimes'=>$rewardTimes?$rewardTimes:0,'rewardTotal'=>$rewardTotal?$rewardTotal:0];
    }

    /**
     * 获取用户所有已发布的任务数
     * @param $uid
     * @return mixed
     */
    static function getUserTaskNum($uid)
    {
        $task_num = Redis::hget('user_task_info:' . $uid, 'task_count');
        if ($task_num === null) {
            $task_num = UserTaskDetail::getUserTaskNum($uid);
            Redis::hset('user_task_info:' . $uid, 'task_count', $task_num);
        }
        return $task_num;
    }

    /**
     * 获取用户感兴趣总数
     */
    static function getUserPickTotal($uid)
    {
        //$interest = 0;
        //$interest = Redis::hget('user_task_info:' . $uid, 'interest_count');
        //if ($interest === null) {
            $interest = UserTaskDetail::getUserPickTotal($uid);
            //Redis::hset('user_task_info:' . $uid, 'interest_count', $interest);
        //}
        return $interest;
    }

    static function getMaxReward($uid)
    {
        $peakReward = Redis::hget('user_task_info:' . $uid, 'peak_reward');
        if ($peakReward === null) {
            $peakReward = UserTaskDetail::getMaxReward($uid);
            Redis::hset('user_task_info:'.$uid,'peak_reward',$peakReward);
        }
        return $peakReward;
    }

    /**
     * 获取用户处理总时长
     * @param $uid
     */
    static function handleDuration($uid){
        $handleDuration = Redis::hget('user_task_info:' . $uid, 'handle_duration');
        if ($handleDuration === null) {
            $handleDuration=serialize(UserTaskDetail::handleDuration($uid));
            Redis::hset('user_task_info:'.$uid,'handle_duration',$handleDuration);
        }
        return unserialize($handleDuration);
    }
    /**
     * 獲取用戶承諾酬金
     * @param $uid
     * @return mixed
     */
    public static function getPromiseRewards($uid){
        return UserTaskDetail::getPromiseRewards($uid);
    }

    public static function getSponsorViewRecoNum($uid){
        return UserTaskDetail::getSponsorViewRecoNum($uid);
    }

    public static function getSponsorDetail($uid){
        $RewardsTimesSum =   self::getUserRewardsInfo($uid);
        $UserInterest =  self::getUserPickTotal($uid);
        $rewardTimes = $RewardsTimesSum['rewardTimes'];
        $rewardTotal = $RewardsTimesSum['rewardTotal'];
        $SponsorViewRecoNum =self::getSponsorViewRecoNum($uid);
        $rewardRate = $SponsorViewRecoNum==0 ? 0:round(100*round($RewardsTimesSum['rewardTimes']/$SponsorViewRecoNum,4),2);
        $taskNum = self::getUserTaskNum($uid);
        $duration_info = self::handleDuration($uid);
        $handleTimes = $duration_info['totalCount'];
        $handleDuration = round($duration_info['totalTime']/3600, 1);
        $handleDurationRate = $handleTimes==0?0:round($handleDuration/$handleTimes,2);
        $rewardAverage = $rewardTimes==0?0:round($RewardsTimesSum['rewardTotal']/$rewardTimes,2);
        //$SponsorViewRecoNum =self::getSponsorViewRecoNum($uid);

        //用户平均信息
        $averageRewardInfo = UserAverageDetailService::getUserRewardsInfo();
        $averageRewardTimes = $averageRewardInfo['rewardTimes'];
        $averageUserInterest =  UserAverageDetailService::getUserPickTotal();
        $averageRewardRate = $averageUserInterest==0?0:round(100*round($averageRewardTimes/$averageUserInterest,4),2);
        $average_duration_info = UserAverageDetailService::handleAllDuration();
        //Log::info('klllll'.$average_duration_info['totalTime']);
        $averageHandleDuration = round($average_duration_info['totalTime']/3600,1);
        $averageHandleTimes = $average_duration_info['totalCount'];

        $averageHandleDurationRate = $averageHandleTimes==0?0:round($averageHandleDuration/$averageHandleTimes,2);

        $rewardRateDiff = $rewardRate-$averageRewardRate;
        $rewardRatelevel = 'medium';
        if($rewardRateDiff>1) $rewardRatelevel = 'high';
        if($rewardRateDiff<-1) $rewardRatelevel = 'low';

        $handleDurationRateDiff = $handleDurationRate-$averageHandleDurationRate;
        $handleDurationRatelevel = 'medium';
        if($handleDurationRateDiff>1) $handleDurationRatelevel = 'high';
        if($handleDurationRateDiff<-1) $handleDurationRatelevel = 'low';

        $rewardTimesDiff = $rewardTimes - $averageRewardTimes;
        $rewardTimeslevel = 'medium';
        if($rewardTimesDiff>1) $rewardTimeslevel = 'high';
        if($rewardTimesDiff<-1) $rewardTimeslevel = 'low';

        return [
            'rewardRate'=>$rewardRate,
            'rewardRatelevel'=>$rewardRatelevel,
            'handleDurationRate'=>$handleDurationRate<1?'<1':$handleDurationRate,
            'handleDurationRatelevel'=>$handleDurationRatelevel,
            'rewardTimes'=>$rewardTimes,
            'rewardTimeslevel'=>$rewardTimeslevel,
            'taskNum'=>$taskNum,
            'rewardTotal'=>$rewardTotal,
            'rewardAverage'=>$rewardAverage
        ];
    }

    /**
     * 获取用户平均打赏
     * @param $uid 打赏发起者人
     * @return array
     */
    public static function getAverageReward($uid){
        $RewardToUserSum = Redis::hget('user_task_info:' . $uid, 'RewardToUserSum');
        $RewardToUserNum = Redis::hget('user_task_info:' . $uid, 'RewardToUserNum');
        if($RewardToUserNum===null || $RewardToUserSum===null){
            $res = UserTaskDetail::getAverageReward($uid);
            $RewardToUserSum = $res['RewardToUserSum'];
            $RewardToUserNum = $res['RewardToUserNum'];
            Redis::hset('user_task_info:' . $uid, 'RewardToUserSum',$RewardToUserSum);
            Redis::hset('user_task_info:' . $uid, 'RewardToUserNum',$RewardToUserNum);
        }
        return [
            'RewardToUserNum'=>$RewardToUserNum,
            'RewardToUserSum'=>$RewardToUserSum
        ];
    }

    /**
     * 获取用户被打赏次数
     * @param $uid
     * @return mixed
     */
    public static function getRewardedTimes($uid){
        return UserTaskDetail::getRewardedTimes($uid);
    }
}